package com.zhnc.dangjianpingtaiapp.gui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.argum.GridDataHelp;
import com.zhnc.dangjianpingtaiapp.argum.ListDataHelp;
import com.zhnc.dangjianpingtaiapp.gui.base.BaseFragment;

import java.util.ArrayList;

/**
 * @author wangdh
 * @time 2019/4/2 13:50
 * @describe
 */
public class HomeDangWuFragment extends BaseFragment {
    private Banner banner;
    private FrameLayout ll_content;
    private FrameLayout ll_content2;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_dang_wu, container, false);
    }

    public void re(Fragment fragment, int layout) {

        FragmentTransaction fragmentTransaction = this.getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(layout, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        banner = getView().findViewById(R.id.banner);
        ll_content = getView().findViewById(R.id.ll_content);
        ll_content2 = getView().findViewById(R.id.ll_content2);


        ItemGridFragment itemGridFragment = new ItemGridFragment();
        itemGridFragment.setRely(GridDataHelp._dangwu(getActivity(), itemGridFragment));
        re(itemGridFragment, R.id.ll_content);

        ItemListFragment itemListFragment = new ItemListFragment();
        itemListFragment.setRely(ListDataHelp.getDW(getActivity(), itemGridFragment));
        re(itemListFragment, R.id.ll_content2);


        ArrayList<String> strings = new ArrayList<>();
        ArrayList<String> titles = new ArrayList<>();
        titles.add("1");
        titles.add("2");
        titles.add("3");

        strings.add("" + R.drawable.banner1);
        strings.add("" + R.drawable.banner2);
        strings.add("" + R.drawable.banner3);

        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Picasso.get().load(Integer.valueOf((String) path)).into(imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Toast.makeText(StandbyActivity.this,"点击了",Toast.LENGTH_SHORT).show();
                    }
                });

            }

            //提供createImageView 方法，如果不用可以不重写这个方法，主要是方便自定义ImageView的创建
            @Override
            public ImageView createImageView(Context context) {
                ImageView simpleDraweeView = new ImageView(context);
                return simpleDraweeView;
            }
        });
        //设置图片集合
        banner.setImages(strings);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }
}
